﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit;
using NUnit.Framework;
using wne.Context;
using wne.core;
using wne.models;

/*
 * IsValidUserPass - Sletov +
 * IsAdmin - Korpak +
 * ChangeUserPassword - Detenkov - Ilin +
 */

namespace wne.Tests
{
    [TestFixture]
    public class AccountFacadeTest
    {
        #region DataBase
        //просмотреть базу
        [Test]
        public void DataBase_Viewer()
        {
            using (var context = new tpksEntities())
            {
                var allusers = context.User.ToList();
                var allcontacts = context.Contact.ToList();
               // var allgroups = context.Group.ToList();
                //var allGroupFields = context.Group_Field.ToList();
               // var allRelations = context.Group_Relation.ToList();
                //var allSteps = context.Step.ToList();
                //var allSteps_prm = context.Step_Prm.ToList();
                //var allStep_fileds = context.Step_Field.ToList();
                //var allWays = context.Way.ToList();
                //var allways_param = context.Way_Prm.ToList();
                //var alldocs = context.Doc.ToList();

                int a = 0;
            }

        }

        //заполнение базы
        [Test]
        public void DataBase_fill_Group()
        {
            using (var context = new tpksEntities())
            {
                // Группы
                Group group1 = new Group(); // 1096
                group1.Group_Name = "Users";
                group1.Parent_Flag = "0";
                context.Group.Add(group1);

                Group group2 = new Group(); // 1097
                group2.Group_Name = "Admins";
                group2.Parent_Flag = "1";
                context.Group.Add(group2);

                Group group3 = new Group(); // 1098
                group3.Group_Name = "All users";
                group3.Parent_Flag = "1";
                context.Group.Add(group3);

                Group group4 = new Group(); // 1099
                group4.Group_Name = "Guests";
                group4.Parent_Flag = "0";
                context.Group.Add(group4);

                Group group5 = new Group(); // 1100
                group5.Group_Name = "Baned users";
                group5.Parent_Flag = "0";
                context.Group.Add(group5);

                context.SaveChanges();
            }
            
        }
        [Test]
        public void DataBase_fill_Group_Field()
        {
            using (var context = new tpksEntities())
            {
                // Поля групп
                Group_Field field1 = new Group_Field();
                field1.Field_Name = "Date_of_registration";
                field1.Group_ID = 1096;
                field1.Field_Type = 6;
                field1.Field_Value = "";
                field1.Mandatory_Flag = "1";
                field1.User_Edit_Flag = "0";
                context.Group_Field.Add(field1);

                Group_Field field2 = new Group_Field();
                field2.Field_Name = "Ratig";
                field2.Group_ID = 1096;
                field2.Field_Type = 2;
                field2.Field_Value = "";
                field2.Mandatory_Flag = "0";
                field2.User_Edit_Flag = "0";
                context.Group_Field.Add(field2);

                Group_Field field3 = new Group_Field();
                field3.Field_Name = "Number_of_baned_users";
                field3.Group_ID = 1097;
                field3.Field_Type = 2;
                field3.Field_Value = "";
                field3.Mandatory_Flag = "0";
                field3.User_Edit_Flag = "0";
                context.Group_Field.Add(field3);

                Group_Field field4 = new Group_Field();
                field4.Field_Name = "Ratig";
                field4.Group_ID = 1097;
                field4.Field_Type = 2;
                field4.Field_Value = "";
                field4.Mandatory_Flag = "0";
                field4.User_Edit_Flag = "0";
                context.Group_Field.Add(field4);

                Group_Field field6 = new Group_Field();
                field6.Field_Name = "Date_of_last_visit";
                field6.Group_ID = 1098;
                field6.Field_Type = 6;
                field6.Field_Value = "";
                field6.Mandatory_Flag = "1";
                field6.User_Edit_Flag = "0";
                context.Group_Field.Add(field6);

                Group_Field field5 = new Group_Field();
                field5.Field_Name = "Number_of_visits";
                field5.Group_ID = 1099;
                field5.Field_Type = 2;
                field5.Field_Value = "";
                field5.Mandatory_Flag = "1";
                field5.User_Edit_Flag = "0";
                context.Group_Field.Add(field5);

                Group_Field field7 = new Group_Field();
                field7.Field_Name = "Date_of_ban";
                field7.Group_ID = 1100;
                field7.Field_Type = 6;
                field7.Field_Value = "";
                field7.Mandatory_Flag = "1";
                field7.User_Edit_Flag = "0";
                context.Group_Field.Add(field7);
                
                context.SaveChanges();
            }
        }

        [Test]
        public void DataBase_fill_Group_Relations()
        {
            using (var context = new tpksEntities())
            {
                Group_Relation rel1 = new Group_Relation();
                rel1.Child_Group_ID = 1096;
                rel1.Parent_Group_ID = 1098;
                context.Group_Relation.Add(rel1);

                Group_Relation rel2 = new Group_Relation();
                rel2.Child_Group_ID = 1097;
                rel2.Parent_Group_ID = 1098;
                context.Group_Relation.Add(rel2);

                Group_Relation rel3 = new Group_Relation();
                rel3.Child_Group_ID = 1099;
                rel3.Parent_Group_ID = 1098;
                context.Group_Relation.Add(rel3);

                Group_Relation rel4 = new Group_Relation();
                rel4.Child_Group_ID = 1100;
                rel4.Parent_Group_ID = 1096;
                context.Group_Relation.Add(rel4);

                context.SaveChanges();
            }

        }

        [Test]
        public void DataBase_fill_User()
        {
            using (var context = new tpksEntities())
            {

                var group_admins = context.Group.Include("Group_Field").FirstOrDefault(x => x.Group_ID.Equals(1061));
                var group_users = context.Group.Include("Group_Field").FirstOrDefault(x => x.Group_ID.Equals(1060));
                var group_allusers = context.Group.Include("Group_Field").FirstOrDefault(x => x.Group_ID.Equals(1062));
                // Пользователи
                User user1 = new User();
                user1.First_Name = "Name1";
                user1.Second_Name = "Second_Name1";
                user1.Last_Name = "Last_Name1";
                user1.Login = "Login1";
                user1.Password = "Pass1";
                user1.ICQ = 111111;
                user1.Profession = "Prof1";
                user1.Birth_Date = new DateTime(1991, 1, 1);
                user1.Admin_Flag = "1";             
                user1.Group.Add(group_admins);
 
                context.User.Add(user1);

                User user2 = new User();
                user2.First_Name = "Name2";
                user2.Second_Name = "Second_Name2";
                user2.Last_Name = "Last_Name2";
                user2.Login = "Login2";
                user2.Password = "Pass2";
                user2.ICQ = 222222;
                user2.Profession = "Prof2";
                user2.Birth_Date = new DateTime(1992, 2, 2);
                user2.Admin_Flag = "1";
                user2.Group.Add(group_admins);
                context.User.Add(user2);



                User user3 = new User();
                user3.First_Name = "Name3";
                user3.Second_Name = "Second_Name3";
                user3.Last_Name = "Last_Name3";
                user3.Login = "Login3";
                user3.Password = "Pass3";
                user3.ICQ = 333333;
                user3.Profession = "Prof3";
                user3.Birth_Date = new DateTime(1993,3,3);
                user3.Admin_Flag = "0";
                user3.Group.Add(group_users);
                context.User.Add(user3);

                User user4 = new User();
                user4.First_Name = "Name4";
                user4.Second_Name = "Second_Name4";
                user4.Last_Name = "Last_Name4";
                user4.Login = "Login4";
                user4.Password = "Pass4";
                user4.ICQ = 444444;
                user4.Profession = "Prof4";
                user4.Birth_Date = new DateTime(1994, 4, 4);
                user4.Admin_Flag = "0";
                user4.Group.Add(group_users);
                context.User.Add(user4);

                User user5 = new User();
                user5.First_Name = "Name5";
                user5.Second_Name = "Second_Name5";
                user5.Last_Name = "Last_Name5";
                user5.Login = "Login5";
                user5.Password = "Pass5";
                user5.ICQ = 555555;
                user5.Profession = "Prof5";
                user5.Birth_Date = new DateTime(1995, 5, 5);
                user5.Admin_Flag = "0";
                user5.Group.Add(group_users);
                context.User.Add(user5);

                context.SaveChanges();
            }

        }
        

        [Test]
        public void DataBase_Group_Delete()
        {
            var adfacad = new AdminFacadeSql();
            adfacad.RemoveGroup(1068);
            for (int i = 1101; i < 1300; i++)
            {
                adfacad.RemoveGroup(i);
            }
        }

        [Test]
        public void DataBase_User_Delete()
        {
            var adfacad = new AdminFacadeSql();
            for (int i = 1000; i < 1048; i++)
            {
                adfacad.RemoveUser(i);
            }
        

        /*using (var context = new tpksEntities())
            {
                context.Group.Remove(context.Group.FirstOrDefault(x => x.Group_ID.Equals(1020)));
                context.SaveChanges();
            }*/

        }

        #endregion
        
        //Tests:

        #region IsValidUserPass
        [Test]
        public void IsValidUserPass_Is_Valid()
        {
            var acfacade = new core.AccountFacadeSql();
            Assert.IsTrue(acfacade.IsValidUserPass("Login1", "Pass1"));
        }

        [Test]
        public void IsValidUserPass_Is_not_Valid()
        {
            var acfacade = new core.AccountFacadeSql();
            Assert.IsFalse(acfacade.IsValidUserPass("Login1", "wewewewe"));
        }
        
        [Test]
        public void IsValidUserPass_user_Is_not_exists()
        {
            var acfacade = new core.AccountFacadeSql();
            Assert.IsFalse(acfacade.IsValidUserPass("qwe", "Pass1"));
        }

        [Test]
        public void IsValidUserPass_user_with_foreign_pass()
        {
            var acfacade = new core.AccountFacadeSql();
            Assert.IsFalse(acfacade.IsValidUserPass("Login2", "Pass1"));
        }


        #endregion

        #region IsAdmin

        [Test]
        public void IsAdmin_admin_name_true_pass()
        {
            var acfs = new core.AccountFacadeSql();
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1048);
                Assert.IsTrue(acfs.IsAdmin(user1.Login) && user1.Admin_Flag.Equals("1"));
            }
        }

        [Test]
        public void IsAdmin_not_admin_name_true_pass()
        {
            var acfs = new core.AccountFacadeSql();
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1051);
                Assert.IsFalse(acfs.IsAdmin(user1.Login));
            }
        }

        [Test]
        public void IsAdmin_admin_name_another_pass()
        {
            var acfs = new core.AccountFacadeSql();
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1048);
                User user2 = context.User.Find(1051);
                Assert.IsFalse(acfs.IsAdmin(user1.Login));
            }
        }
        [Test]
        public void IsAdmin_admin_name_wrong_pass()
        {
            var acfs = new core.AccountFacadeSql();
            using (var context = new tpksEntities())
            {
                User user1 = context.User.Find(1048);
                Assert.IsFalse(acfs.IsAdmin(user1.Login));
            }
        }
        [Test]
        public void IsAdmin_not_exist()
        {
            var acfs = new core.AccountFacadeSql();
            using (var context = new tpksEntities())
            {
                Assert.IsFalse(acfs.IsAdmin("asdsww2"));
            }
        }
        #endregion

        #region ChangeUserPassword

        [Test]
        public void ChangeUserPassword_change()
        {
            using (var context = new tpksEntities())
            {
                var acfacade = new AccountFacadeSql();
                Assert.IsTrue(acfacade.ChangeUserPassword("Login1", "NewPass3", "Pass1"));
                User user1 = context.User.Find(1048);
                Assert.IsTrue(user1.Password == "Pass1");
            }
        }

        [Test]
        public void ChangeUserPassword_user_is_not_exist()
        {
            using (var context = new tpksEntities())
            {
                var acfacade = new AccountFacadeSql();
                Assert.IsFalse(acfacade.ChangeUserPassword("Notexist", "Pass1", "NewPass3"));
            }
        }

        [Test]
        public void ChangeUserPassword_equal()
        {
            using (var context = new tpksEntities())
            {
                var acfacade = new AccountFacadeSql();
                Assert.IsTrue(acfacade.ChangeUserPassword("Login1", "Pass1", "Pass1"));
            }
        }

        [Test]
        public void ChangeUserPassword_wrong()
        {
            using (var context = new tpksEntities())
            {
                var acfacade = new AccountFacadeSql();
                Assert.IsFalse(acfacade.ChangeUserPassword("Login1", "WrongPass", "NewPass3"));
            }
        }
        #endregion

    }
}
